<template>
  <div class="app-container">
    <div class="search-bar">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item>
          <el-button type="primary" @click="handleQuery">
            <template #icon>
              <Search/>
            </template>
            搜索
          </el-button>
          <el-button @click="handleResetQuery">
            <template #icon>
              <Refresh/>
            </template>
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never">
      <div class="mb-10px">
        <el-button
            v-hasPerm="['system:lpsOrderYl:add']"
            type="success"
            @click="handleOpenDialog()"
        >
          <template #icon>
            <Plus/>
          </template>
          新增
        </el-button>
        <el-button
            v-hasPerm="['system:lpsOrderYl:delete']"
            type="danger"
            :disabled="removeIds.length === 0"
            @click="handleDelete()"
        >
          <template #icon>
            <Delete/>
          </template>
          删除
        </el-button>
      </div>

      <el-table
          ref="dataTableRef"
          v-loading="loading"
          :data="pageData"
          highlight-current-row
          border
          @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column
            key="cgdd"
            label="采购订单"
            prop="cgdd"
            min-width="150"
            align="center"
            :show-overflow-tooltip="true"
        />
        <el-table-column
            key="poxxm"
            label="PO行项目"
            prop="poxxm"
            min-width="150"
            align="center"
            :show-overflow-tooltip="true"
        />
        <el-table-column
            key="xsdd"
            label="销售订单"
            prop="xsdd"
            min-width="150"
            align="center"
            :show-overflow-tooltip="true"
        />
        <el-table-column
            key="soxxm"
            label="SO行项目"
            prop="soxxm"
            min-width="150"
            align="center"
            :show-overflow-tooltip="true"
        />
        <el-table-column
            key="wl"
            label="物料"
            prop="wl"
            min-width="150"
            align="center"
            :show-overflow-tooltip="true"
        />
        <el-table-column
            key="ms"
            label="描述"
            prop="ms"
            min-width="150"
            align="center"
            :show-overflow-tooltip="true"
        />
        <el-table-column
            key="thbb"
            label="图号/版本"
            prop="thbb"
            min-width="150"
            align="center"
            :show-overflow-tooltip="true"
        />
        <el-table-column
            key="sl"
            label="数量"
            prop="sl"
            min-width="150"
            align="center"
            :show-overflow-tooltip="true"
        />
        <el-table-column
            key="jhrq"
            label="交货日期"
            prop="jhrq"
            min-width="150"
            align="center"
            :show-overflow-tooltip="true"
        />
        <el-table-column
            key="bk"
            label="BK"
            prop="bk"
            min-width="150"
            align="center"
            :show-overflow-tooltip="true"
        />
        <el-table-column
            key="tk"
            label="TK"
            prop="tk"
            min-width="150"
            align="center"
            :show-overflow-tooltip="true"
        />
        <el-table-column
            key="tkwcLeft"
            label="TKWC_LEFT"
            prop="tkwcLeft"
            min-width="150"
            align="center"
            :show-overflow-tooltip="true"
        />
        <el-table-column
            key="tkwoLeft"
            label="TKWO_LEFT"
            prop="tkwoLeft"
            min-width="150"
            align="center"
            :show-overflow-tooltip="true"
        />
        <el-table-column
            key="tkwcRight"
            label="TKWC_RIGHT"
            prop="tkwcRight"
            min-width="150"
            align="center"
            :show-overflow-tooltip="true"
        />
        <el-table-column
            key="tkwoRight"
            label="TKWO_RIGHT"
            prop="tkwoRight"
            min-width="150"
            align="center"
            :show-overflow-tooltip="true"
        />
        <el-table-column
            key="grpWallBaseTypeLeft"
            label="GRP_WALL_BASE_TYPE_LEFT"
            prop="grpWallBaseTypeLeft"
            min-width="250"
            align="center"
            :show-overflow-tooltip="true"
        />
        <el-table-column
            key="grpWallBaseTypeRight"
            label="GRP_WALL_BASE_TYPE_RIGHT"
            prop="grpWallBaseTypeRight"
            min-width="255"
            align="center"
            :show-overflow-tooltip="true"
        />
        <el-table-column
            key="grpCopTkt1"
            label="GRP_COP_TKT1 "
            prop="grpCopTkt1"
            min-width="150"
            align="center"
            :show-overflow-tooltip="true"
        />
        <el-table-column
            key="grpCopTkt2"
            label="GRP_COP_TKT2"
            prop="grpCopTkt2"
            min-width="150"
            align="center"
            :show-overflow-tooltip="true"
        />
        <el-table-column
            key="grpCopTkt3"
            label="GRP_COP_TKT3"
            prop="grpCopTkt3"
            min-width="150"
            align="center"
            :show-overflow-tooltip="true"
        />
        <el-table-column
            key="grpCopTkt4"
            label="GRP_COP_TKT4"
            prop="grpCopTkt4"
            min-width="150"
            align="center"
            :show-overflow-tooltip="true"
        />
        <el-table-column
            key="kja"
            label="孔距A"
            prop="kja"
            min-width="150"
            align="center"
            :show-overflow-tooltip="true"
        />
        <el-table-column
            key="kjb"
            label="孔距B"
            prop="kjb"
            min-width="150"
            align="center"
            :show-overflow-tooltip="true"
        />
        <el-table-column
            key="fszd"
            label="扶手长度(L)"
            prop="fszd"
            min-width="150"
            align="center"
            :show-overflow-tooltip="true"
        />
        <el-table-column
            key="gdzjsl"
            label="固定支架数量"
            prop="gdzjsl"
            min-width="150"
            align="center"
            :show-overflow-tooltip="true"
        />
        <el-table-column
            key="bmcl"
            label="表面处理"
            prop="bmcl"
            min-width="150"
            align="center"
            :show-overflow-tooltip="true"
        />
        <el-table-column
            key="bz"
            label="备注"
            prop="bz"
            width="300"
            align="center"
            :show-overflow-tooltip="true"
        />
      </el-table>
      <pagination
          v-if="total > 0"
          v-model:total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="handleQuery()"
      />
    </el-card>

    <!-- 永乐表单弹窗 -->
    <el-dialog
        v-model="dialog.visible"
        :title="dialog.title"
        width="500px"
        @close="handleCloseDialog"
    >
      <el-form ref="dataFormRef" :model="formData" :rules="rules" label-width="100px">
        <el-form-item label="采购订单" prop="cgdd">
          <el-input
              v-model="formData.cgdd"
              placeholder="采购订单"
          />
        </el-form-item>

        <el-form-item label="PO行项目" prop="poxxm">
          <el-input
              v-model="formData.poxxm"
              placeholder="PO行项目"
          />
        </el-form-item>

        <el-form-item label="销售订单" prop="xsdd">
          <el-input
              v-model="formData.xsdd"
              placeholder="销售订单"
          />
        </el-form-item>

        <el-form-item label="SO行项目" prop="soxxm">
          <el-input
              v-model="formData.soxxm"
              placeholder="SO行项目"
          />
        </el-form-item>

        <el-form-item label="物料" prop="wl">
          <el-input
              v-model="formData.wl"
              placeholder="物料"
          />
        </el-form-item>

        <el-form-item label="描述" prop="ms">
          <el-input
              v-model="formData.ms"
              placeholder="描述"
          />
        </el-form-item>

        <el-form-item label="图号/版本" prop="thbb">
          <el-input
              v-model="formData.thbb"
              placeholder="图号/版本"
          />
        </el-form-item>

        <el-form-item label="数量" prop="sl">
          <el-input
              v-model="formData.sl"
              placeholder="数量"
          />
        </el-form-item>

        <el-form-item label="交货日期" prop="jhrq">
          <el-input
              v-model="formData.jhrq"
              placeholder="交货日期"
          />
        </el-form-item>

        <el-form-item label="BK" prop="bk">
          <el-input
              v-model="formData.bk"
              placeholder="BK"
          />
        </el-form-item>

        <el-form-item label="TK" prop="tk">
          <el-input
              v-model="formData.tk"
              placeholder="TK"
          />
        </el-form-item>

        <el-form-item label="TKWC_LEFT" prop="tkwcLeft">
          <el-input
              v-model="formData.tkwcLeft"
              placeholder="TKWC_LEFT"
          />
        </el-form-item>

        <el-form-item label="TKWO_LEFT" prop="tkwoLeft">
          <el-input
              v-model="formData.tkwoLeft"
              placeholder="TKWO_LEFT"
          />
        </el-form-item>

        <el-form-item label="TKWC_RIGHT" prop="tkwcRight">
          <el-input
              v-model="formData.tkwcRight"
              placeholder="TKWC_RIGHT"
          />
        </el-form-item>

        <el-form-item label="TKWO_RIGHT" prop="tkwoRight">
          <el-input
              v-model="formData.tkwoRight"
              placeholder="TKWO_RIGHT"
          />
        </el-form-item>

        <el-form-item label="GRP_WALL_BASE_TYPE_LEFT" prop="grpWallBaseTypeLeft">
          <el-input
              v-model="formData.grpWallBaseTypeLeft"
              placeholder="GRP_WALL_BASE_TYPE_LEFT"
          />
        </el-form-item>

        <el-form-item label="GRP_WALL_BASE_TYPE_RIGHT" prop="grpWallBaseTypeRight">
          <el-input
              v-model="formData.grpWallBaseTypeRight"
              placeholder="GRP_WALL_BASE_TYPE_RIGHT"
          />
        </el-form-item>

        <el-form-item label="GRP_COP_TKT1 " prop="grpCopTkt1">
          <el-input
              v-model="formData.grpCopTkt1"
              placeholder="GRP_COP_TKT1 "
          />
        </el-form-item>

        <el-form-item label="GRP_COP_TKT2" prop="grpCopTkt2">
          <el-input
              v-model="formData.grpCopTkt2"
              placeholder="GRP_COP_TKT2"
          />
        </el-form-item>

        <el-form-item label="GRP_COP_TKT3" prop="grpCopTkt3">
          <el-input
              v-model="formData.grpCopTkt3"
              placeholder="GRP_COP_TKT3"
          />
        </el-form-item>

        <el-form-item label="GRP_COP_TKT4" prop="grpCopTkt4">
          <el-input
              v-model="formData.grpCopTkt4"
              placeholder="GRP_COP_TKT4"
          />
        </el-form-item>

        <el-form-item label="扶手长度(L)" prop="fszd">
          <el-input
              v-model="formData.fszd"
              placeholder="扶手长度(L)"
          />
        </el-form-item>

        <el-form-item label="固定支架数量" prop="gdzjsl">
          <el-input
              v-model="formData.gdzjsl"
              placeholder="固定支架数量"
          />
        </el-form-item>

        <el-form-item label="表面处理" prop="bmcl">
          <el-input
              v-model="formData.bmcl"
              placeholder="表面处理"
          />
        </el-form-item>

        <el-form-item label="备注" prop="bz">
          <el-input
              v-model="formData.bz"
              placeholder="备注"
          />
        </el-form-item>

      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="handleSubmit()">确定</el-button>
          <el-button @click="handleCloseDialog()">取消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
defineOptions({
  name: "LpsOrderYl",
  inheritAttrs: false,
});

import LpsOrderYlAPI, {LpsOrderYlForm, LpsOrderYlPageQuery, LpsOrderYlPageVO} from "@/api/system/lps-order-yl";

const queryFormRef = ref(ElForm);
const dataFormRef = ref(ElForm);

const loading = ref(false);
const removeIds = ref<number[]>([]);
const total = ref(0);

const queryParams = reactive<LpsOrderYlPageQuery>({
  pageNum: 1,
  pageSize: 10,
});

// 永乐表格数据
const pageData = ref<LpsOrderYlPageVO[]>([]);

// 弹窗
const dialog = reactive({
  title: "",
  visible: false,
});

// 永乐表单数据
const formData = reactive<LpsOrderYlForm>({});

// 永乐表单校验规则
const rules = reactive({});

/** 查询永乐 */
function handleQuery() {
  loading.value = true;
  LpsOrderYlAPI.getPage(queryParams)
      .then((data) => {
        pageData.value = data.list;
        total.value = data.total;
      })
      .finally(() => {
        loading.value = false;
      });
}

/** 重置永乐查询 */
function handleResetQuery() {
  queryFormRef.value!.resetFields();
  queryParams.pageNum = 1;
  handleQuery();
}

/** 行复选框选中记录选中ID集合 */
function handleSelectionChange(selection: any) {
  removeIds.value = selection.map((item: any) => item.id);
}

/** 打开永乐弹窗 */
function handleOpenDialog(id?: number) {
  dialog.visible = true;
  if (id) {
    dialog.title = "修改永乐";
    LpsOrderYlAPI.getFormData(id).then((data) => {
      Object.assign(formData, data);
    });
  } else {
    dialog.title = "新增永乐";
  }
}

/** 提交永乐表单 */
function handleSubmit() {
  dataFormRef.value.validate((valid: any) => {
    if (valid) {
      loading.value = true;
      const id = formData.id;
      if (id) {
        LpsOrderYlAPI.update(id, formData)
            .then(() => {
              ElMessage.success("修改成功");
              handleCloseDialog();
              handleResetQuery();
            })
            .finally(() => (loading.value = false));
      } else {
        LpsOrderYlAPI.add(formData)
            .then(() => {
              ElMessage.success("新增成功");
              handleCloseDialog();
              handleResetQuery();
            })
            .finally(() => (loading.value = false));
      }
    }
  });
}

/** 关闭永乐弹窗 */
function handleCloseDialog() {
  dialog.visible = false;
  dataFormRef.value.resetFields();
  dataFormRef.value.clearValidate();
  formData.id = undefined;
}

/** 删除永乐 */
function handleDelete(id?: number) {
  const ids = [id || removeIds.value].join(",");
  if (!ids) {
    ElMessage.warning("请勾选删除项");
    return;
  }

  ElMessageBox.confirm("确认删除已选中的数据项?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(
      () => {
        loading.value = true;
        LpsOrderYlAPI.deleteByIds(ids)
            .then(() => {
              ElMessage.success("删除成功");
              handleResetQuery();
            })
            .finally(() => (loading.value = false));
      },
      () => {
        ElMessage.info("已取消删除");
      }
  );
}

onMounted(() => {
  handleQuery();
});
</script>
